SlideShare a Scribd company logo
Class No.05  Data Structures http://ecomputernotes.com
Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3;  for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; }  http://ecomputernotes.com
Josephus Problem Using a circularly-linked list made the solution trivial. http://ecomputernotes.com
Josephus Problem Using a circularly-linked list made the solution trivial. The solution would have been more difficult if an array had been used. http://ecomputernotes.com
Josephus Problem Using a circularly-linked list made the solution trivial. The solution would have been more difficult if an array had been used. This illustrates the fact that the choice of the appropriate data structures can significantly simplify an algorithm. It can make the algorithm much faster and efficient. http://ecomputernotes.com
Josephus Problem Using a circularly-linked list made the solution trivial. The solution would have been more difficult if an array had been used. This illustrates the fact that the choice of the appropriate data structures can significantly simplify an algorithm. It can make the algorithm much faster and efficient. Later we will see how some elegant data structures lie at the heart of major algorithms. http://ecomputernotes.com
Josephus Problem Using a circularly-linked list made the solution trivial. The solution would have been more difficult if an array had been used. This illustrates the fact that the choice of the appropriate data structures can significantly simplify an algorithm. It can make the algorithm much faster and efficient. Later we will see how some elegant data structures lie at the heart of major algorithms. An entire CS course “Design and Analysis of Algorithms” is devoted to this topic. http://ecomputernotes.com
Abstract Data Type We have looked at four different implementations of the List data structures: Using arrays Singly linked list Doubly linked list Circularly linked list. http://ecomputernotes.com
Abstract Data Type We have looked at four different implementations of the List data structures: Using arrays Singly linked list Doubly linked list Circularly linked list. The interface to the List stayed the same, i.e., add(), get(), next(), start(), remove() etc. http://ecomputernotes.com
Abstract Data Type We have looked at four different implementations of the List data structures: Using arrays Singly linked list Doubly linked list Circularly linked list. The interface to the List stayed the same, i.e., add(), get(), next(), start(), remove() etc. The list is thus an abstract data type; we use it without being concerned with how it is implemented. http://ecomputernotes.com
Abstract Data Type What we care about is the methods that are available for use with the List ADT. http://ecomputernotes.com
Abstract Data Type What we care about is the methods that are available for use with the List ADT. We will follow this theme when we develop other ADT.  http://ecomputernotes.com
Abstract Data Type What we care about is the methods that are available for use with the List ADT. We will follow this theme when we develop other ADT.  We will publish the interface and keep the freedom to change the implementation of ADT without effecting users of the ADT. http://ecomputernotes.com
Abstract Data Type What we care about is the methods that are available for use with the List ADT. We will follow this theme when we develop other ADT.  We will publish the interface and keep the freedom to change the implementation of ADT without effecting users of the ADT. The C++ classes provide us the ability to create such ADTs. http://ecomputernotes.com
Stacks Stacks in real life: stack of books, stack of plates Add new items at the top Remove an item at the top Stack data structure similar to real life: collection of elements arranged in a linear order. Can only access element at the top  http://ecomputernotes.com
Stack Operations Push(X) – insert X as the top element of the stack Pop() – remove the top element of the stack and return it. Top() – return the top element without removing it from the stack.  http://ecomputernotes.com
Stack Operations push(2) top 2 push(5) top 2 5 push(7) top 2 5 7 push(1) top 2 5 7 1 1 pop() top 2 5 7 push(21) top 2 5 7 21 21 pop() top 2 5 7 7 pop() 2 5 top 5 pop() 2 top http://ecomputernotes.com
Stack Operation The last element to go into the stack is the first to come out:  LIFO  – Last In First Out. What happens if we call pop() and there is no element? Have IsEmpty() boolean function that returns true if stack is empty, false otherwise. Throw StackEmpty exception: advanced C++ concept. http://ecomputernotes.com
Stack Implementation: Array  Worst case for insertion and deletion from an array when insert and delete from the beginning: shift elements to the left. Best case for insert and delete is at the end of the array – no need to shift any elements. Implement push() and pop() by inserting and deleting at the end of an array. http://ecomputernotes.com
Stack using an Array top 2 5 7 1 2 5 7 1 0 1 3 2 4 top = 3 http://ecomputernotes.com
Stack using an Array In case of an array, it is possible that the array may “fill-up” if we push enough elements. Have a boolean function  IsFull()  which returns true is stack (array) is full, false otherwise. We would call this function before calling push(x). http://ecomputernotes.com
Stack Operations with Array int pop() { return A[current--]; } void push(int x) { A[++current] = x; } http://ecomputernotes.com
Stack Operations with Array int top() { return A[current]; }  int IsEmpty() { return ( current == -1 ); } int IsFull() { return ( current == size-1); } A quick examination shows that all five operations take constant time. http://ecomputernotes.com
Stack Using Linked List We can avoid the size limitation of a stack implemented with an array by using a linked list to hold the stack elements. As with array, however, we need to decide where to insert elements in the list and where to delete them so that push and pop will run the fastest. http://ecomputernotes.com

More Related Content

PPT
Computer notes - Josephus Problem
PPSX
Data structure stack&queue basics
PPT
Stacks overview with its applications
PPT
Stack linked list
PPT
computer notes - Data Structures - 2
PPT
data structure, stack, stack data structure
PPTX
My lecture stack_queue_operation
PPSX
Data Structure (Stack)
Computer notes - Josephus Problem
Data structure stack&queue basics
Stacks overview with its applications
Stack linked list
computer notes - Data Structures - 2
data structure, stack, stack data structure
My lecture stack_queue_operation
Data Structure (Stack)

What's hot (19)

PPTX
Stack and Queue by M.Gomathi Lecturer
PDF
Data structure week y 5
PDF
Stacks and queues
PPT
Lecture2
PPTX
Introduction to stack
PPT
Lecture4
PPT
Lecture3
PPTX
Stack and queue
PPT
Queue data structure
PPT
Stack a Data Structure
PPTX
Stack Data structure
PPT
Queue in Data Structure
PPT
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
PPTX
Ppt presentation of queues
PPT
Chapter 10: hashing data structure
PPT
Queue Data Structure
PDF
computer notes - Priority queue
PPTX
Stack data structure
PPT
Queue Data Structure
Stack and Queue by M.Gomathi Lecturer
Data structure week y 5
Stacks and queues
Lecture2
Introduction to stack
Lecture4
Lecture3
Stack and queue
Queue data structure
Stack a Data Structure
Stack Data structure
Queue in Data Structure
Fallsem2015 16 cp1699-20-jul-2015_rm01_stacks_and_queues
Ppt presentation of queues
Chapter 10: hashing data structure
Queue Data Structure
computer notes - Priority queue
Stack data structure
Queue Data Structure
Ad

Viewers also liked (20)

PPT
computer notes - Data Structures - 10
PPT
computer notes - Data Structures - 14
PPT
Chapter 13 - Recursion
PPT
Recursion and looping
PPT
computer notes - Data Structures - 30
PPT
4 recursion details
PPT
Computer notes data structures - 9
PPT
Ch06 Stack
PDF
computer notes - Recursion
DOC
e computer notes - Recursion
PDF
Module 01 Stack and Recursion
PPT
Computer notes - Analysis of Union
PPT
PPTX
The Stack And Recursion
PPTX
Recursion
PPTX
Recursion
PPT
Computer notes - Recursive
PPT
Data Structures- Part5 recursion
PPT
C++ Memory Management
PPT
Recursion
computer notes - Data Structures - 10
computer notes - Data Structures - 14
Chapter 13 - Recursion
Recursion and looping
computer notes - Data Structures - 30
4 recursion details
Computer notes data structures - 9
Ch06 Stack
computer notes - Recursion
e computer notes - Recursion
Module 01 Stack and Recursion
Computer notes - Analysis of Union
The Stack And Recursion
Recursion
Recursion
Computer notes - Recursive
Data Structures- Part5 recursion
C++ Memory Management
Recursion
Ad

Similar to computer notes - Data Structures - 5 (20)

PDF
computer notes - Circular list
PPTX
Stack in Sata Structure
PPTX
Data Structure
PPT
Funddamentals of data structures
PPTX
هياكلبيانات
PPTX
DATA STRUCTURE AND COMPUTER ALGORITHMS LECTURE 2
PPT
Lecture5
PPT
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
PDF
Data Structures & Algorithm design using C
PDF
Datastructure
PDF
Chapter 5 Stack and Queue.pdf
PPTX
stack_operationss_documentation_file.ppt
PPTX
EC2311 – Data Structures and C Programming
PPT
Fundamentals of data structures
PPTX
Data_structure.pptx
PPTX
Data Structures Algorithms and Applications
PDF
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
PPTX
Stack and its applications
PPTX
Data Structure & Algorithm.pptx
PPT
lecture 02.2.ppt
computer notes - Circular list
Stack in Sata Structure
Data Structure
Funddamentals of data structures
هياكلبيانات
DATA STRUCTURE AND COMPUTER ALGORITHMS LECTURE 2
Lecture5
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
Data Structures & Algorithm design using C
Datastructure
Chapter 5 Stack and Queue.pdf
stack_operationss_documentation_file.ppt
EC2311 – Data Structures and C Programming
Fundamentals of data structures
Data_structure.pptx
Data Structures Algorithms and Applications
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Stack and its applications
Data Structure & Algorithm.pptx
lecture 02.2.ppt

More from ecomputernotes (20)

PPT
computer notes - Data Structures - 39
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 20
PPT
computer notes - Data Structures - 15
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 31
PPT
computer notes - Data Structures - 4
PPT
computer notes - Data Structures - 13
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 16
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 35
PPT
computer notes - Data Structures - 36
DOC
Computer notes - Enhancements to the GROUP BY Clause
computer notes - Data Structures - 39
computer notes - Data Structures - 11
computer notes - Data Structures - 20
computer notes - Data Structures - 15
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 28
computer notes - Data Structures - 19
computer notes - Data Structures - 31
computer notes - Data Structures - 4
computer notes - Data Structures - 13
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 16
computer notes - Data Structures - 22
computer notes - Data Structures - 35
computer notes - Data Structures - 36
Computer notes - Enhancements to the GROUP BY Clause

Recently uploaded (20)

PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
MYSQL Presentation for SQL database connectivity
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Review of recent advances in non-invasive hemoglobin estimation
DOCX
The AUB Centre for AI in Media Proposal.docx
Digital-Transformation-Roadmap-for-Companies.pptx
20250228 LYD VKU AI Blended-Learning.pptx
cuic standard and advanced reporting.pdf
MIND Revenue Release Quarter 2 2025 Press Release
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
NewMind AI Weekly Chronicles - August'25 Week I
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
Advanced methodologies resolving dimensionality complications for autism neur...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
MYSQL Presentation for SQL database connectivity
“AI and Expert System Decision Support & Business Intelligence Systems”
Review of recent advances in non-invasive hemoglobin estimation
The AUB Centre for AI in Media Proposal.docx

computer notes - Data Structures - 5

  • 1. Class No.05 Data Structures http://ecomputernotes.com
  • 2. Josephus Problem #include &quot;CList.cpp&quot; void main(int argc, char *argv[]) { CList list; int i, N=10, M=3; for(i=1; i <= N; i++ ) list.add(i); list.start(); while( list.length() > 1 ) { for(i=1; i <= M; i++ ) list.next(); cout << &quot;remove: &quot; << list.get() << endl; list.remove(); } cout << &quot;leader is: &quot; << list.get() << endl; }  http://ecomputernotes.com
  • 3. Josephus Problem Using a circularly-linked list made the solution trivial. http://ecomputernotes.com
  • 4. Josephus Problem Using a circularly-linked list made the solution trivial. The solution would have been more difficult if an array had been used. http://ecomputernotes.com
  • 5. Josephus Problem Using a circularly-linked list made the solution trivial. The solution would have been more difficult if an array had been used. This illustrates the fact that the choice of the appropriate data structures can significantly simplify an algorithm. It can make the algorithm much faster and efficient. http://ecomputernotes.com
  • 6. Josephus Problem Using a circularly-linked list made the solution trivial. The solution would have been more difficult if an array had been used. This illustrates the fact that the choice of the appropriate data structures can significantly simplify an algorithm. It can make the algorithm much faster and efficient. Later we will see how some elegant data structures lie at the heart of major algorithms. http://ecomputernotes.com
  • 7. Josephus Problem Using a circularly-linked list made the solution trivial. The solution would have been more difficult if an array had been used. This illustrates the fact that the choice of the appropriate data structures can significantly simplify an algorithm. It can make the algorithm much faster and efficient. Later we will see how some elegant data structures lie at the heart of major algorithms. An entire CS course “Design and Analysis of Algorithms” is devoted to this topic. http://ecomputernotes.com
  • 8. Abstract Data Type We have looked at four different implementations of the List data structures: Using arrays Singly linked list Doubly linked list Circularly linked list. http://ecomputernotes.com
  • 9. Abstract Data Type We have looked at four different implementations of the List data structures: Using arrays Singly linked list Doubly linked list Circularly linked list. The interface to the List stayed the same, i.e., add(), get(), next(), start(), remove() etc. http://ecomputernotes.com
  • 10. Abstract Data Type We have looked at four different implementations of the List data structures: Using arrays Singly linked list Doubly linked list Circularly linked list. The interface to the List stayed the same, i.e., add(), get(), next(), start(), remove() etc. The list is thus an abstract data type; we use it without being concerned with how it is implemented. http://ecomputernotes.com
  • 11. Abstract Data Type What we care about is the methods that are available for use with the List ADT. http://ecomputernotes.com
  • 12. Abstract Data Type What we care about is the methods that are available for use with the List ADT. We will follow this theme when we develop other ADT. http://ecomputernotes.com
  • 13. Abstract Data Type What we care about is the methods that are available for use with the List ADT. We will follow this theme when we develop other ADT. We will publish the interface and keep the freedom to change the implementation of ADT without effecting users of the ADT. http://ecomputernotes.com
  • 14. Abstract Data Type What we care about is the methods that are available for use with the List ADT. We will follow this theme when we develop other ADT. We will publish the interface and keep the freedom to change the implementation of ADT without effecting users of the ADT. The C++ classes provide us the ability to create such ADTs. http://ecomputernotes.com
  • 15. Stacks Stacks in real life: stack of books, stack of plates Add new items at the top Remove an item at the top Stack data structure similar to real life: collection of elements arranged in a linear order. Can only access element at the top http://ecomputernotes.com
  • 16. Stack Operations Push(X) – insert X as the top element of the stack Pop() – remove the top element of the stack and return it. Top() – return the top element without removing it from the stack. http://ecomputernotes.com
  • 17. Stack Operations push(2) top 2 push(5) top 2 5 push(7) top 2 5 7 push(1) top 2 5 7 1 1 pop() top 2 5 7 push(21) top 2 5 7 21 21 pop() top 2 5 7 7 pop() 2 5 top 5 pop() 2 top http://ecomputernotes.com
  • 18. Stack Operation The last element to go into the stack is the first to come out: LIFO – Last In First Out. What happens if we call pop() and there is no element? Have IsEmpty() boolean function that returns true if stack is empty, false otherwise. Throw StackEmpty exception: advanced C++ concept. http://ecomputernotes.com
  • 19. Stack Implementation: Array Worst case for insertion and deletion from an array when insert and delete from the beginning: shift elements to the left. Best case for insert and delete is at the end of the array – no need to shift any elements. Implement push() and pop() by inserting and deleting at the end of an array. http://ecomputernotes.com
  • 20. Stack using an Array top 2 5 7 1 2 5 7 1 0 1 3 2 4 top = 3 http://ecomputernotes.com
  • 21. Stack using an Array In case of an array, it is possible that the array may “fill-up” if we push enough elements. Have a boolean function IsFull() which returns true is stack (array) is full, false otherwise. We would call this function before calling push(x). http://ecomputernotes.com
  • 22. Stack Operations with Array int pop() { return A[current--]; } void push(int x) { A[++current] = x; } http://ecomputernotes.com
  • 23. Stack Operations with Array int top() { return A[current]; } int IsEmpty() { return ( current == -1 ); } int IsFull() { return ( current == size-1); } A quick examination shows that all five operations take constant time. http://ecomputernotes.com
  • 24. Stack Using Linked List We can avoid the size limitation of a stack implemented with an array by using a linked list to hold the stack elements. As with array, however, we need to decide where to insert elements in the list and where to delete them so that push and pop will run the fastest. http://ecomputernotes.com

Editor's Notes

  • #3: End of lecture 4. The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #4: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #5: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #6: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #7: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #8: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #9: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #10: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #11: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #12: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #13: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #14: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #15: The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • #16: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #17: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #18: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #19: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #20: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #21: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #22: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #23: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #24: A primary concern for this course is efficiency. You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.
  • #25: End of lecture 5 You might believe that faster computers make it unnecessary to be concerned with efficiency. However… So we need special training.